<template>
  <div class="age-calculator">
    <div class="container">
      <h2>年龄计算器</h2>
      
      <div class="calculator-box">
        <div class="input-section">
          <el-config-provider :locale="zhCn">
            <el-date-picker
              v-model="birthDate"
              type="date"
              v-bind="datePickerConfig"
              @change="calculateAge"
            />
          </el-config-provider>
          <el-button type="primary" @click="calculateAge">
            <el-icon><Calendar /></el-icon>
            计算
          </el-button>
        </div>

        <div class="result-section" v-if="hasResult">
          <div class="result-table">
            <div class="result-row">
              <div class="label">周岁：</div>
              <div class="value">{{ results.years }}</div>
            </div>
            <div class="result-row">
              <div class="label">已生活天数：</div>
              <div class="value">{{ results.days }}</div>
            </div>
            <div class="result-row">
              <div class="label">已生活分钟数：</div>
              <div class="value">{{ results.minutes }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离50岁还有：</div>
              <div class="value">{{ results.to50 }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离60岁还有：</div>
              <div class="value">{{ results.to60 }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离70岁还有：</div>
              <div class="value">{{ results.to70 }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离80岁还有：</div>
              <div class="value">{{ results.to80 }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离90岁还有：</div>
              <div class="value">{{ results.to90 }}</div>
            </div>
            <div class="result-row">
              <div class="label">距离100岁还有：</div>
              <div class="value">{{ results.to100 }}</div>
            </div>
            <div class="result-row">
              <div class="label">下次生日：</div>
              <div class="value" v-html="results.nextBirthday"></div>
            </div>
          </div>
        </div>

        <div class="help-text">
          <h3>帮助说明：</h3>
          <p>在线年龄计算器：本工具支持输入出生日期（阳历）查询年龄生日相关信息。</p>
        </div>

        <div class="back-home">
          <el-button @click="goHome">
            <el-icon><HomeFilled /></el-icon>
            返回首页
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import dayjs from 'dayjs'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { useRouter } from 'vue-router'
import { Calendar, HomeFilled } from '@element-plus/icons-vue'

const router = useRouter()

const birthDate = ref('1988-07-15')
const hasResult = ref(false)
const results = reactive({
  years: '',
  days: '',
  minutes: '',
  to50: '',
  to60: '',
  to70: '',
  to80: '',
  to90: '',
  to100: '',
  nextBirthday: ''
})

const datePickerConfig = {
  placeholder: '请选择出生日期',
  format: 'YYYY-MM-DD',
  valueFormat: 'YYYY-MM-DD',
  defaultValue: new Date('1988-07-15'),
  locale: zhCn,
}

const calculateAge = () => {
  if (!birthDate.value) return
  
  const birth = dayjs(birthDate.value)
  const now = dayjs()
  
  const years = now.diff(birth, 'year')
  const days = now.diff(birth, 'day') + 1
  const minutes = now.diff(birth, 'minute')
  
  const calculateTimeToAge = (targetAge) => {
    const targetDate = birth.add(targetAge, 'year')
    if (now.isAfter(targetDate)) return '已过'
    
    const totalMonths = targetDate.diff(now, 'month')
    const years = Math.floor(totalMonths / 12)
    const months = totalMonths % 12
    const remainDays = targetDate.diff(now, 'day') + 1
    
    let timeStr = ''
    if (years > 0) {
      timeStr += `${years}年`
      if (months > 0) {
        timeStr += `${months}个月`
      }
    } else {
      timeStr = `${months}个月`
    }
    
    return `${timeStr}（${remainDays}天）`
  }
  
  const nextBirthday = () => {
    const thisYearBirthday = dayjs(`${now.year()}-${birth.format('MM-DD')}`)
    const nextYear = thisYearBirthday.isAfter(now) ? thisYearBirthday : thisYearBirthday.add(1, 'year')
    const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    const daysToNext = nextYear.diff(now, 'day') + 1
    return `${nextYear.format('YYYY-MM-DD')} ${weekDays[nextYear.day()]}, 还有 <span class="highlight">${daysToNext}</span> 天`
  }

  results.years = years
  results.days = days
  results.minutes = minutes
  results.to50 = calculateTimeToAge(50)
  results.to60 = calculateTimeToAge(60)
  results.to70 = calculateTimeToAge(70)
  results.to80 = calculateTimeToAge(80)
  results.to90 = calculateTimeToAge(90)
  results.to100 = calculateTimeToAge(100)
  results.nextBirthday = nextBirthday()
  
  hasResult.value = true
}

const goHome = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.age-calculator {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;

  h2 {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
  }

  .calculator-box {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
    padding: 20px;

    .input-section {
      display: flex;
      gap: 20px;
      margin-bottom: 30px;
      justify-content: center;
      align-items: center;
    }

    .result-section {
      margin-bottom: 30px;

      .result-table {
        width: 100%;
        border: 1px solid #ebeef5;
        border-radius: 4px;

        .result-row {
          display: flex;
          border-bottom: 1px solid #ebeef5;
          
          &:last-child {
            border-bottom: none;
          }

          .label {
            width: 150px;
            padding: 12px 15px;
            background: #f5f7fa;
            border-right: 1px solid #ebeef5;
            font-weight: bold;
          }

          .value {
            flex: 1;
            padding: 12px 15px;
            text-align: left;
            font-size: 16px;

            :deep(.highlight) {
              color: #19be6b;
              font-weight: bold;
              margin: 0 4px;
            }
          }
        }
      }
    }

    .help-text {
      background: #f5f7fa;
      padding: 15px;
      border-radius: 4px;
      text-align: left;

      h3 {
        color: #67c23a;
        margin: 0 0 10px 0;
        font-size: 16px;
      }

      p {
        color: #666;
        margin: 0;
        line-height: 1.6;
      }
    }

    .back-home {
      text-align: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;

      .el-button {
        width: 200px;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .age-calculator {
    padding: 10px;

    h2 {
      font-size: 20px;
      margin-bottom: 20px;
    }

    .calculator-box {
      padding: 15px;

      .input-section {
        flex-direction: column;
        gap: 15px;

        :deep(.el-date-editor) {
          width: 100%;
        }

        .el-button {
          width: 100%;
          height: 40px;
        }
      }

      .result-section {
        .result-table {
          .result-row {
            .label {
              width: 120px;
              padding: 10px;
              font-size: 14px;
            }

            .value {
              padding: 10px;
              font-size: 14px;
            }
          }
        }
      }

      .help-text {
        padding: 12px;
        
        h3 {
          font-size: 14px;
        }

        p {
          font-size: 13px;
        }
      }

      .back-home {
        margin-top: 15px;
        padding-top: 15px;

        .el-button {
          width: 100%;
          height: 40px;
        }
      }
    }
  }
}
</style> 